<template>
    <!-- 主页页面布局参考element，在element.js中引入相应组件 在el-container中新加一个类名 加一个高度-->
  <el-container class="home-container">
   <el-header>
     <div>
       <img src="../assets/heima.png" alt="logo">
       <span>阿哲的电商后台管理系统</span>
     </div>
    <el-button type="info" @click="release">退出</el-button>
   </el-header>
  <el-container>
    <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409EFF" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
      <div class="toggle-button" @click="toggleMenu">|||</div>
      <el-submenu :index="item.id + ''" v-for="item in Menulist" :key="item.id">
        <template slot="title">
          <i :class="iconsObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNative('/' + subItem.path)">
          <i class="el-icon-menu"></i>
          <span>{{subItem.authName}}</span>
        </el-menu-item>
      </el-submenu>
    </el-menu>
      </el-aside>
  <el-main>
    <router-view></router-view>
  </el-main>
  </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧数据菜单
      Menulist: [],
      // 创建一个字体图标库
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 定义折叠状态
      isCollapse: false,
      // 定义被激活的链接地址
      activePath: ''
    }
  },
  created () {
  // 在生命周期函数中调用 数据
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    release () {
    // 第一步 清空token
      window.sessionStorage.clear()
      // 第二步 跳转到登录页面
      this.$router.push('/login')
    },
    // 在方法中定义一个函数 获取后台数据渲染左侧列表 使用axios请求后台数据
    async getMenus () {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      // 判断一下状态码的状态 获取不成功返回错误新提示
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 等于200 成功获取候赋值
      this.Menulist = res.data
    },
    // 折叠事件,
    toggleMenu () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活地址
    saveNative (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
  // 主页全局样式设置背景色
  .home-container {
    height: 100%;
    .el-header {
      background-color: #373d41;
     display: flex;
     justify-content: space-between;
     padding-left: 0px;
     color: #fff;
     align-items: center;
     font-size: 20px;
     div {
       display: flex;
       align-items: center;
       span {
         margin-left: 15px;
       }
     }
    }
    .el-aside {
      background-color: #333744;
      .el-menu {
          border-right: none;
        }
      .toggle-button {
        background-color: #4a5064;
        font-size: 10px;
        line-height: 24px;
        text-align: center;
        color: #fff;
        letter-spacing: 0.2em;
        cursor: pointer;
      }
      .iconfont {
        margin-right: 10px;
      }
    }
    .el-main {
      background-color: #eaedf1;
    }

  }
</style>
